package com.jade.shoesmallinterface.controller;

import com.jade.shoesmallinterface.entity.Order;
import com.jade.shoesmallinterface.entity.Result;
import com.jade.shoesmallinterface.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoes/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    /**
     * 根据用户ID查询订单信息
     */
    @GetMapping("/selectOrdersByUserId")
    public Result<List<Order>> selectOrdersByUserId(@RequestParam Integer user_id) {
        if (user_id == null) {
            return Result.error("用户ID不能为空");
        }
        List<Order> orders = orderService.selectOrdersByUserId(user_id);
        return Result.success(orders);
    }

    /**
     * 增加订单
     */
    @PostMapping("/insertOrder")
    public Result<Integer> insertOrder(@RequestBody Order order) {
        if (order == null) {
            return Result.error("订单信息不能为空");
        }
        int result = orderService.insertOrder(order);
        return Result.success(result);
    }

    /**
     * 更新订单数据（部分更新）
     */
    @PostMapping("/updateOrder")
    public Result<Integer> updateOrder(@RequestParam Integer id, @RequestBody Order order) {
        System.out.println("id"+id);
        if (id == null) {
            return Result.error("订单ID不能为空");
        }
        // 设置ID，确保更新到正确的订单
        order.setId(id);
        int result = orderService.updateOrder(id, order);
        if (result > 0) {
            return Result.success(result);
        } else {
            return Result.error("订单更新失败，可能订单ID不存在");
        }
    }
}